import axios from 'axios'
import { message } from 'antd'

// axios config
axios.defaults.timeout = 5000
axios.defaults.baseURL = '/api/api'

// 拦截请求
axios.interceptors.request.use(
  config => {
    let token = window.localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

// 拦截响应
axios.interceptors.response.use(
  response => {
    return response
  },
  err => {
    if (err.response) {
      switch (err.response.status) {
        case 401:
          window.localStorage.setItem('token', '')
          message.warn('token有问题')
          // 路由转发 /login
      }
    }
    return Promise.reject(err.response.data)
  }
)

export default axios
